{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6b84de0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方案1 把字典中的项变为元组后排序\n",
    "# 方案2 使用sorted函数的key参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "77a7fd12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 35,\n",
       " 'b': 71,\n",
       " 'c': 87,\n",
       " 'd': 16,\n",
       " 'e': 60,\n",
       " 'f': 7,\n",
       " 'g': 75,\n",
       " 'h': 74,\n",
       " 'i': 85,\n",
       " 'j': 52,\n",
       " 'k': 3}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#生成学生成绩\n",
    "from random import randint\n",
    "s = { name:randint(1,100) for name in 'abcdefghijk'}\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f3a07c6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(35, 'a'),\n",
       " (71, 'b'),\n",
       " (87, 'c'),\n",
       " (16, 'd'),\n",
       " (60, 'e'),\n",
       " (7, 'f'),\n",
       " (75, 'g'),\n",
       " (74, 'h'),\n",
       " (85, 'i'),\n",
       " (52, 'j'),\n",
       " (3, 'k')]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方案1 把字典中的项变为元组后排序\n",
    "#使用列表推导生成元组列表\n",
    "st=[(v,k) for k,v in s.items()]\n",
    "st"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5cfd7cf4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(35, 'a'),\n",
       " (71, 'b'),\n",
       " (87, 'c'),\n",
       " (16, 'd'),\n",
       " (60, 'e'),\n",
       " (7, 'f'),\n",
       " (75, 'g'),\n",
       " (74, 'h'),\n",
       " (85, 'i'),\n",
       " (52, 'j'),\n",
       " (3, 'k')]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用zip\n",
    "st2 = list(zip(s.values(),s.keys()))\n",
    "st2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8850b86d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(87, 'c'),\n",
       " (85, 'i'),\n",
       " (75, 'g'),\n",
       " (74, 'h'),\n",
       " (71, 'b'),\n",
       " (60, 'e'),\n",
       " (52, 'j'),\n",
       " (35, 'a'),\n",
       " (16, 'd'),\n",
       " (7, 'f'),\n",
       " (3, 'k')]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用sorted排序\n",
    "sorted(st,reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b2e65399",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('c', 87),\n",
       " ('i', 85),\n",
       " ('g', 75),\n",
       " ('h', 74),\n",
       " ('b', 71),\n",
       " ('e', 60),\n",
       " ('j', 52),\n",
       " ('a', 35),\n",
       " ('d', 16),\n",
       " ('f', 7),\n",
       " ('k', 3)]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方案2 使用sorted函数的key参数\n",
    "sorted(s.items(),key=lambda item:item[1],reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be682425",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
